/* Copyright (c) 2009 Fabrice Desré
 */

const Cc = Components.classes;
const Ci = Components.interfaces;

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function widgetURIPolicy() {
    // If you only need to access your component from Javascript, uncomment the
    // following line:
    //this.wrappedJSObject = this;
}

widgetURIPolicy.prototype = {
    
    classDescription: "Implements content policy to prevent cross widget access",
    
    classID:          Components.ID("{ff795b25-dad0-43c5-9da2-7ffe3a6343f8}"),
    contractID:       "@desre.org/widget-uri-policy;1",
    _xpcom_categories: [{ category: "content-policy"}],
    
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy]),
    
    shouldLoad : function(aContentType, aContentLocation, aRequestOrigin, aContext,
                          aMimeTypeGuess, aExtra) {
        if (aContentLocation.scheme == "widget" && aRequestOrigin && aRequestOrigin.scheme == "widget") {
            if (aContentLocation.host == aRequestOrigin.host)
              return Ci.nsIContentPolicy.ACCEPT;
            else
              return Ci.nsIContentPolicy.REJECT_OTHER; // maybe use REJECT_SERVER ?
        }
        return Ci.nsIContentPolicy.ACCEPT;
    },
    
    shouldProcess : function(aContentType, aContentLocation, aRequestOrigin, aContext,
                             aMimeType, aExtra) {
        return Ci.nsIContentPolicy.ACCEPT;
    }

};

// XPCOM registration of class.
var components = [widgetURIPolicy];
function NSGetModule(compMgr, fileSpec) {
    return XPCOMUtils.generateModule(components);
}
